﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace durable.xml.ConfigProvider
{
    public partial class XmlProvider : Provider
    {
        public  string _XmlAPIPath
        {
            get;set;
        }


        public override void Delete(string code)
        {
            if (File.Exists(_XmlAPIPath))
            {
                XElement xe = XElement.Load(_XmlAPIPath);
                IEnumerable<XElement> elements = from ele in xe.Elements("Config")
                                                 where (string)ele.Element("code") == code
                                                 select ele;
                {
                    if (elements.Count() > 0)
                        elements.First().Remove();
                }
                xe.Save(_XmlAPIPath);
            }
        }
      
        public override void Update(dynamic p)
        {
            if (File.Exists(_XmlAPIPath))
            {
                XElement xe = XElement.Load(_XmlAPIPath);
                IEnumerable<XElement> elements = from ele in xe.Elements("Config")
                                                 where (string)ele.Element("code") == p["code"]
                                                 select ele;
                {
                    if (elements.Count() > 0)
                    {

                        XElement first = elements.First();
                        List<object> list = new List<object>();
                        foreach (KeyValuePair<string, string> item in p)
                        {
                            list.Add(new XElement(item.Key, item.Value));

                        }
                        first.ReplaceNodes(
                          list.ToArray()

                            );
                    }

                }
                xe.Save(_XmlAPIPath);
            }
        }
        public override List<dynamic> Fill()
        {
            if (!File.Exists(_XmlAPIPath))
                return null;
            List<dynamic> listp = new List<dynamic>();
            XElement xe = XElement.Load(_XmlAPIPath);
            IEnumerable<XElement> elements = from ele in xe.Elements("Config")
                                             select ele;
            foreach (var ele in elements)
            {
                Dictionary<string, string> p = new Dictionary<string, string>();
                foreach (var attribute in ele.Elements())
                {
                    p.Add(attribute.Name.LocalName, attribute.Value);
                }                   
                listp.Add(p);
            }
            return listp;
        }
        public override List<dynamic> Fill(string name,string value)
        {
            if (!File.Exists(_XmlAPIPath))
                return null;
            List<dynamic> listp = new List<dynamic>();
            XElement xe = XElement.Load(_XmlAPIPath);
            IEnumerable<XElement> elements = from ele in xe.Elements("Config")
                                             where (string)ele.Element(name) == value
                                             select ele;
            foreach (var ele in elements)
            {
                Dictionary<string, string> p = new Dictionary<string, string>();
                foreach (var attribute in ele.Elements())
                {
                    p.Add(attribute.Name.LocalName, attribute.Value);
                }
                listp.Add(p);
            }
            return listp;
        }
        public override dynamic Entity(string code)
        {
            if (!File.Exists(_XmlAPIPath))
                return null;
            Dictionary<string, string> p = new Dictionary<string, string>();
            XElement xe = XElement.Load(_XmlAPIPath);
            IEnumerable<XElement> elements = from ele in xe.Elements("Config")
                                             where (string)ele.Element("code") == code
                                             select ele;
            foreach (var ele in elements)
            {
                foreach (var attribute in ele.Elements())
                {
                    p.Add(attribute.Name.LocalName, attribute.Value);
                }

            }
            return p;

        }

        public override void Insert(dynamic p)
        {
            string dir = Path.GetDirectoryName(_XmlAPIPath);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            if (!File.Exists(_XmlAPIPath))
            {
                using (XmlTextWriter writer = new XmlTextWriter(_XmlAPIPath, Encoding.UTF8))
                {
                    writer.Formatting = Formatting.Indented;
                    writer.Indentation = 4;
                    writer.WriteStartDocument(true);
                    writer.WriteStartElement("Configs");
                    writer.WriteStartElement("Config");
                    foreach (KeyValuePair<string, string> item in p)
                    {
                        writer.WriteElementString(item.Key, item.Value);
                    }                                  
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
            }
            else
            {
               XElement xe = XElement.Load(_XmlAPIPath);
                IEnumerable<XElement> elements = from ele in xe.Elements("Config")
                                                 where (string)ele.Element("code") == p["code"]
                                                 select ele;
                if (elements.Count() > 0)
                {
                    Update(p);

                }
                else
                {
                    List<object> list = new List<object>();

                    foreach (KeyValuePair<string, string> item in p)
                    {
                        list.Add(new XElement(item.Key, item.Value));

                    }

                    XElement record = new XElement(
                       new XElement("Config", list.ToArray()));
                    xe.Add(record);
                    xe.Save(_XmlAPIPath);
                }
            }
        }
    }
}
